using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using FireStarter.CodeGeneration;
using FireStarter.CodeGeneration.Generator;
using FireStarter.CodeGeneration.Parser;
using NUnit.Framework;

namespace FireStarter.Test.CodeGeneration.Parser
{
    [TestFixture]
    public class TestIfExpressionParser
    {
        private static readonly List<IActionParser> _defaultActionParsers = new List<IActionParser>() { new ValueExpressionParser(), new IfExpressionParser() };

        [Test]
        public void Test_Parse()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string innerContent = TestUtilsCodeGen.GetRandomString();
            string text = string.Format("IF({0}):{1}", propertyName, innerContent);
            IParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            TemplateExpression templateExpression = parser.Parse(ref text, _defaultActionParsers);
            //---------------Test Result -----------------------
            IfExpression ifExpression;
            ParserTestUtils.AssertExpressionIsIf(propertyName, templateExpression, out ifExpression);
            ParserTestUtils.AssertExpressionIsLiteral(innerContent, ifExpression.Part);
            Assert.IsNull(ifExpression.Next);
            Assert.AreEqual("", text);
        }

        [Test]
        public void Test_IsExpressionMatch()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string innerContent = TestUtilsCodeGen.GetRandomString();
            string text = string.Format("IF({0}):{1}", propertyName, innerContent);
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsTrue(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_WithWhitespaceBeforeColon()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string text = string.Format("IF({0}){1}:{2}", propertyName, TestUtilsCodeGen.GetRandomWhiteSpace(), TestUtilsCodeGen.GetRandomString());
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsTrue(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_WithNotIndicator()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string text = string.Format("IF(!{0}){1}:{2}", propertyName, TestUtilsCodeGen.GetRandomWhiteSpace(), TestUtilsCodeGen.GetRandomString());
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsTrue(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_WithEqualsClause()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string text = string.Format(@"IF(!{0} EQUALS ""Hello""){1}:{2}", propertyName, TestUtilsCodeGen.GetRandomWhiteSpace(), TestUtilsCodeGen.GetRandomString());
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsTrue(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_Fails_IfJustIf()
        {
            //---------------Set up test pack-------------------
            const string text = "IF";
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsFalse(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_Fails_IfNoColon()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string innerContent = TestUtilsCodeGen.GetRandomString();
            string text = string.Format("IF({0}){1}", propertyName, innerContent);
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsFalse(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_Fails_IfPatternDoesntMatch()
        {
            //---------------Set up test pack-------------------
            string propertyName = TestUtilsCodeGen.GetRandomString();
            string text = propertyName;
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsFalse(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_Fails_IfNull()
        {
            //---------------Set up test pack-------------------
            const string text = null;
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsFalse(isExpressionMatch);
        }

        [Test]
        public void Test_IsExpressionMatch_Fails_IfEmpty()
        {
            //---------------Set up test pack-------------------
            const string text = "";
            IfExpressionParser parser = new IfExpressionParser();
            //-------------Assert Preconditions -------------

            //---------------Execute Test ----------------------
            bool isExpressionMatch = parser.IsExpressionMatch(text);
            //---------------Test Result -----------------------
            Assert.IsFalse(isExpressionMatch);
        }
    }
}